package Records.Attributes;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import Records.Attributes.LibraryDataSets;
import Records.Attributes.LibraryMetadataExtract;
import Records.Database.UpdateLibraryImp;
import Records.Main.RecordsMain;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author Alexander
 */
public class ImportSongs {

    static void walkTree(File topFile, List<File> fileList) {
        System.out.println("ImportSongs.walkTree()");

        if (topFile.isDirectory()) {
            for (File f : topFile.listFiles()) {
                walkTree(f, fileList);
            }
        } else if (topFile.isFile()) {
            if (FilenameUtils.isExtension(topFile.getAbsolutePath(), "m4a") || FilenameUtils.isExtension(topFile.getAbsolutePath(), "mp3")) {
                fileList.add(topFile);
                RecordsMain.addToFilePathList(topFile.getAbsolutePath());
            }
        }
    }

    public static void importSongs(String Dir) {
        System.out.println("ImportSongs.importSongs(" + Dir + ")");
        ArrayList<LibraryDataSets> datasets = new ArrayList<>();

        List<File> fileList = new LinkedList<>();
        walkTree(new File(Dir), fileList);
        LibraryMetadataExtract meta;
        for (File f : fileList) {
            meta = new LibraryMetadataExtract(f);
            LibraryDataSets LDS = new LibraryDataSets(meta.extract());
            LDS.InsertLibraryData();
        }
        System.out.println("Library Import Complete!");
        UpdateLibraryImp.updater();
    }
}
